#coding=utf8
import sys, os, time,random
import requests, json
import urllib.request
import ssl
import uuid
import re
RETRY_TIME = 3
import logging

proxies = {
        'http': '127.0.0.1:1212',
        'https': '127.0.0.1:1212'
    }
        

SESSION_TIMEOUT = 5 * 60


def gen_uuid():
    s_uuid=str(uuid.uuid4())
    l_uuid=s_uuid.split('-')
    s_uuid=''.join(l_uuid)

    return s_uuid

def create_payment(token, url = "https://api-demo.airwallex.com/api/v1/pa/payment_intents/create"):
    requestID = gen_uuid()
    payLoad= {
        "request_id": f"{requestID}",
        "currency": "USD",
        "amount":"1",
        "merchant_order_id":"picstrick-type1"
        # "customer":{
        #     "email": "john.doe@airwallex.com", "first_name": "John",
        #     "last_name": "Doe", "merchant_customer_id": "Xx1234", "phone_number": "13800000000"
        #     }, "order": {
        #     "products": [{
        #     "code": "8277-44692", "name": "Iphone 14", "desc": "phone",
        #     "quantity": 1,
        #     "sku": "b4121fb64929d818", "type": "physical", "unit_price": "100.00"
        #     }],
        #     "shipping": { "address": {
        #     "country_code": "CN", "state": "Shaanxi",
        #     "city": "Xi'an",
        #     "street": "Cai fu zhong xin", "postcode": "710103"
        #     },
        #     "first_name": "John",
        #     "last_name": "Doe", "email":"126464@qq.com", "phone_number": "15094044362", "shipping_method": "ManualDelivery" },
        #     "type": "physical_goods"
        # } 
    }

    headers = {
        #"Authorization": "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0b20iLCJyb2xlcyI6WyJ1c2VyIl0sImlhdCI6MTQ4ODQxNTI1NywiZXhwIjoxNDg4NDE1MjY3fQ.UHqau03y5kEk5lFbTp7J4a-U6LXsfxIVNEsux85hj-Q",
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }

    response = requests.post(url, payLoad, headers)
    print(response.status_code, response.text)

    if 200 == response.status_code:
        print(response.text.decode("utf-8"))
        return json.loads(response.read())
    
def send_getToken(url="https://api-demo.airwallex.com/api/v1/authentication/login"):

    headers = {
    "Content-Type": "application/json",
    "x-api-key": "6af93320dcb12966bb8feefd6b3ce572960d0614e3feb73f1410804efe6e7b08c9a7672d0882f8c13432f61e3b41c50f",
    "x-client-id": "DF5oVtBLQFG_BAV5l37k1w"
    }

    payLoad = "{}"

    current = 0
    delay = 5
    while current < RETRY_TIME:
        try: 
            current = current + 1
            response = requests.post(url, json=payLoad, headers=headers)
            
            print(response.status_code)
            if 201 == response.status_code:

                print(json.loads(response.text))
                return json.loads(response.text)['expires_at'], json.loads(response.text)['token']
            
            return None, None
          
        except Exception as e:
            logging.exception(e)
            time.sleep(delay)
            delay *= 2

    return None, None

if __name__ == '__main__':
   
   status, token = send_getToken()

   print(status, token)

   print(create_payment(token))